/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.neolokura.travian.helper.batch.logic;

import ar.com.neolokura.travian.helper.troop.entity.TroopDetail;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import org.joda.time.Duration;

/**
 *
 * @author gustavo
 */
public class TravianHelperBatchLogic {

    public TravianHelperBatchLogic() {
    }

    public void waitSeconds(Integer seconds) throws InterruptedException {

        Thread.sleep(1000 * seconds); // do nothing for 1000 miliseconds * seconds)
    }

    public void waitSeconds(Integer minimum, Integer maximum) throws InterruptedException {

        Random randomGenerator = new Random();

        Integer seconds = minimum + randomGenerator.nextInt(maximum - minimum);

        waitSeconds(seconds);
    }

    public String getDuration(Duration duration) {

        Long totalSeconds = duration.getStandardSeconds();

        Long hours = totalSeconds / 3600;

        totalSeconds -= hours * 3600;

        Long minutes = totalSeconds / 60;

        totalSeconds -= minutes * 60;


        String durationString = hours < 10 ? "0" + hours.toString() : hours.toString();
        durationString += ":";
        durationString += minutes < 10 ? "0" + minutes.toString() : minutes.toString();
        durationString += ":";
        durationString += totalSeconds < 10 ? "0" + totalSeconds.toString() : totalSeconds.toString();



        return durationString;
    }

    /**
     * 
     * Compares two Maps and deletes the keys from the mainMap which aren't in the newMap
     * @param mainMap
     * @param newMap
     * @return 
     */
  
    public void backupTroopList(HashMap<String, TroopDetail> troopList) {
    }

    public void cleanOldTroopList(HashMap<String, TroopDetail> troopList) {

        ArrayList<String> removingKeys = new ArrayList<String>();

        for (Entry<String, TroopDetail> entry : troopList.entrySet()) {
            TroopDetail detail = entry.getValue();
            if (detail == null || detail.getArrivalDate() == null || detail.getArrivalDate().isBeforeNow()) {
                removingKeys.add(entry.getKey());
            }
        }
        for (String key : removingKeys) {
            troopList.remove(key);
        }

    }
}
